package com.xbongbong.pro.rabbitmq.consumer;

import com.alibaba.fastjson.JSON;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.message.enums.PushTypeEnum;
import com.xbongbong.pro.message.pojo.dto.MessageRabbitMqDTO;
import com.xbongbong.pro.message.pojo.vo.MessageRabbitMqVO;
import com.xbongbong.pro.rabbitmq.binders.MarketActivityConsumerBinder;
import com.xbongbong.pro.rabbitmq.constant.MarketActivityConstant;
import com.xbongbong.pro.rabbitmq.feign.MarketActivityFeignClient;
import feign.RetryableException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.cloud.stream.annotation.StreamListener;
import org.springframework.messaging.handler.annotation.Header;

import javax.annotation.Resource;

/**
 * 市场管理 消息消费者
 * @author youli.chen
 * @date 2019/2/25 14:04
 * @since v1.0
 * @version v1.0
 */
@EnableBinding(MarketActivityConsumerBinder.class)
public class MarketActivityListener {

    private static Logger logger = LoggerFactory.getLogger(MarketActivityListener.class);

    @Resource
    private MarketActivityFeignClient marketActivityFeignClient;

    @StreamListener(value = MarketActivityConstant.CLUE_INPUT)
    public void clueReceive(MessageRabbitMqDTO message, @Header("type") PushTypeEnum type) {
        XbbResponse<MessageRabbitMqVO> response = null;
        try {
            switch (type) {
                case CLUE_ADD_PUSH:
                    response = marketActivityFeignClient.clueAdd(message);
                    break;
                case CLUE_CHANGE_PUSH:
                    response = marketActivityFeignClient.clueChange(message);
                    break;
                case CLUE_TEAM_ADD_DEL_MAIN:
                    response = marketActivityFeignClient.clueTeamMain(message);
                    break;
                case CLUE_TEAM_ADD_DEL_SYNERGETIC:
                    response = marketActivityFeignClient.clueTeamSynergetic(message);
                    break;
                case CLUE_STAGE:
                    response = marketActivityFeignClient.clueStage(message);
                    break;
                default:
                    break;
            }
            logger.info("MarketActivityListener.Received: "  + type.toString() + "\n message=" + JSON.toJSONString(message));
        } catch (RetryableException e) {
            logger.error("MarketActivityListener.clueReceive RetryableException case by Read timed out, type=" + type);
        } catch (Exception e) {
            logger.error("MarketActivityListener.clueReceive Exception, type=" + type, e);
        }
    }

    @StreamListener(value = MarketActivityConstant.CLUE_HANDOVER_INPUT)
    public void clueHandoverReceive(MessageRabbitMqDTO message, @Header("type") PushTypeEnum type) {
        XbbResponse<MessageRabbitMqVO> response = null;
        try {
            switch (type) {
                case CLUE_HANDOVER_PUSH:
                    response = marketActivityFeignClient.clueHandover(message);
                    break;
                default:
                    break;
            }
            logger.info("MarketActivityListener.Received: "  + type.toString() + "\n message=" + JSON.toJSONString(message));
        } catch (RetryableException e) {
            logger.error("MarketActivityListener.clueHandoverReceive RetryableException case by Read timed out, type=" + type);
        } catch (Exception e) {
            logger.error("MarketActivityListener.clueHandoverReceive Exception, type=" + type, e);
        }

    }

    @StreamListener(value = MarketActivityConstant.CLUE_DISTRIBUTION_INPUT)
    public void clueDistributionReceive(MessageRabbitMqDTO message, @Header("type") PushTypeEnum type) {
        XbbResponse<MessageRabbitMqVO> response = null;
        try {
            switch (type) {
                case CLUE_DISTRIBUTION_PUSH:
                    response = marketActivityFeignClient.clueDistribution(message);
                    break;
                default:
                    break;
            }
            logger.info("MarketActivityListener.Received: "  + type.toString() + "\n message=" + JSON.toJSONString(message));
        } catch (RetryableException e) {
            logger.error("MarketActivityListener.clueDistributionReceive RetryableException case by Read timed out, type=" + type);
        } catch (Exception e) {
            logger.error("MarketActivityListener.clueDistributionReceive Exception, type=" + type, e);
        }
    }
}
